<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<title>${name_china}管理列表</title>
<%@include file="/common/headmy.jsp" %>

<!-- 时间日期插件 -->
<link href="<%=request.getContextPath()%>/static/plugin/My97DatePicker/skin/default/datepicker.css" rel="stylesheet" >
<script src="<%=request.getContextPath()%>/static/plugin/My97DatePicker/WdatePicker.js"></script>

</head>
<body class="iframe-page">
<div class="page-title-wrapper">
    <div class="page-title">
        <div class="page-title-text">
            <span>${name_china}管理</span>
        </div>
        <div id="toolbar" style="float: right"></div>
    </div>
    <div class="page-title-border"></div>
</div>

<div class="search-field">
    <span class="field-cont">
        <span class="field-cont-title">名称</span>
        <input type="text" class="summer-input" id="name"/>
    </span>
    
     <span class="field-cont">
        <span class="field-cont-title">卡类型</span>
        <input type="text" class="summer-input" id="category"/>
    </span>
    
    <!-- <span class="field-cont">
        <span class="field-cont-title">出版开始时间</span>
        <input type="text" class="summer-input" id="startTime" onFocus="var afterTime=$dp.$('stopTime');WdatePicker({dateFmt:'yyyy-MM-dd',onpicked:function(){afterTime.focus();},minDate:'#F{(\'%y-%M-%d\')}',maxDate:'#F{$dp.$D(\'stopTime\')}'})"/>
    </span>
    
    <span class="field-cont">
        <span class="field-cont-title">出版结束时间</span>
        <input type="text" class="summer-input" id="stopTime" onFocus="WdatePicker({dateFmt:'yyyy-MM-dd',minDate:'#F{$dp.$D(\'startTime\')}'})"/>
    </span> -->
    
    
    <span class="field-btn-wrapper">
        <a id="search" class="btn btn-primary btn-icon wave distance-right"><i class="icon icon-search"></i></a>
        <a id="reset" class="btn btn-icon wave wave-dark"><i class="icon icon-refresh"></i></a>
    </span>
</div>

<div id="t-wrap">
    <table class="summer-datagrid" id="dataList">
	    <col width="20%">
	    <col width="20%">
	    <col width="20%">
	    <col width="20%">
	    <col width="10%">
	    <col width="10%">
	    <thead>
		    <tr>
		        <th>名称</th>
		        <th>类别</th>
		        <th>添加日期</th>
				<th>备注</th>
				<th>排序</th>
		        <th>操作</th>
		    </tr>
	    </thead>
    </table>
</div>

<script>
	//请求url
	var CONST_URL = {
		LIST: basePath + '${common_url}list',
	   	INIT_ADD: basePath + "${common_url}initAdd",
	   	INIT_EDIT: basePath + "${common_url}initEdit",
	   	INIT_VIEW: basePath + "${common_url}initView",
	   	DEL: basePath + "${common_url}del",
	   	EXP: basePath + "${common_url}exp",
	   	INIT_IMP: basePath + "${common_url}initImp"
	};
	
    var  permission = CommonUtil.getPermission('${name_english}');
    //alert(JSON.stringify(permission));
    $(function () {
    	//初始化下拉选项
   	   $('#category').combobox({
           valueField : 'code',
           textField : 'name',
           url : COMMON_URL.getCode + '?type=T_WEB_ACTION_MODEL_CATEGORY'
       });
    	
        // 新建toobar
		$.funcbar({
			id: 'toolbar',
			type: 'toolbar',
			data: permission,
			permit: ['${name_english}:initAdd','${name_english}:exp','${name_english}:dynaExp','${name_english}:initImp','${name_english}:wordExp'],
		});
        
        // 查询按钮
        $('#search').on('click', reloadTable);
        
        //重置按钮
        $('#reset').on('click', function () {
        	//重置文本框的值
        	$('#name').val('');
        	$('#category').combobox('clear');
        	//$('#startTime').val(''); 
        	//$('#stopTime').val('');
        	
        	reloadTable();
        });
        
        //新建
        $('#toolbar').on('click', 'a[data-permit="${name_english}:initAdd"]', function() {
        	window.location.href = CONST_URL.INIT_ADD; 	  	
		});
        
        //alert("category == "+$('#category').combobox('getValue'));
        createTable();
        
        //查看
        $('#t-wrap').on('click', 'a[data-permit="${name_english}:initView"]', function() {
        	var $this = $(this),
        	    id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
        	//window.location.href = CONST_URL.INIT_VIEW +"?id=" + id;
        });
       
        //编辑
        $('#t-wrap').on('click', 'a[data-permit="${name_english}:initEdit"]', function() {
            var $this = $(this),
                id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
            window.location.href = CONST_URL.INIT_EDIT + '?id=' + id;
        });	
        
        //删除
		$('#t-wrap').on('click', 'a[data-permit="${name_english}:del"]', function() {
			var $this = $(this),
			    id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
			$.sdialog({
				type: 'confirm',
				msg: '您确定要删除选中的记录吗?',
				onConfirm: function(result) {
				    if (result) {
				    	$.sajax({
			                url : CONST_URL.DEL + "?ids=" + id,
			                onLoadSuccess : function() {
			                	 $.stip({
                	                type:'ok',//显示类型 有info、 ok、 danger
                	                text: '删除成功！'    
			                	});
			                	reloadTable();  
			                }
			            });
				    		
				    }	
				}
			});
		});
        
        //导出 
        $('#toolbar').on('click', 'a[data-permit="${name_english}:exp"]', function() {
        	exp(); 	  	
		});
        
        //导入 
        $('#toolbar').on('click', 'a[data-permit="${name_english}:initImp"]', function() {
        	window.location.href = CONST_URL.INIT_IMP;	
		});
        
    });
    
    // 重新加载表格
	function reloadTable() {
    	//alert($('#category').combobox('getValue'));
		$('#dataList').datagrid('reload',{
            ajax: {
            	param: {
            		"name": trimSpace($('#name').val()),
            		"category":$('#category').combobox('getValue')
                	//"startTime": trimSpace($('#startTime').val()),
                	//"stopTime": trimSpace($('#stopTime').val())
            	}
            }
        });
	}
    
    //创建表格
    function createTable() {
    	$('#dataList').datagrid({
    		ajax: {
    			url: CONST_URL.LIST,
    			param: {
    				"name": trimSpace($('#name').val()),
            		"category":$('#category').combobox('getValue')
                	//"startTime": trimSpace($('#startTime').val()),
                	//"stopTime": trimSpace($('#stopTime').val())
            	}
    		},
    		columns: [
    		     {
    		    	 field: 'id',
    		    	 hidden: true
    		     },
    		     {
    		    	 field: 'name',
    		    	 render: function (rowData) {
    		    		 return '<a href="javascript:;" data-permit="${name_english}:initView" class="link">'+ rowData.name +'</a>';
    		    	 }
    		     },
    		     {
    		    	 field: 'category'
    		     },
    		     {
    		    	 field: 'createTime',
        		    	 render: function (rowData) {
        		    		 return rowData.createTime;
        		    	 }
    		     },
    		     {
    		    	 field: 'note',
    		    	 render: function (rowData) {
    		    		 return rowData.note;
    		    	 }
    		     },
    		     {
    		    	 field: 'reorder'
    		     },
    		    {
    		    	 render: function (rowData) {
    		    		 return $.funcbar({
    		    			 data: permission,    //请求的数据
    		    			 type: 'text',     //类型
    		    			 display: 2,    //显示数量
    		    			 permit: ['${name_english}:initEdit', '${name_english}:del'] //需要的权限按钮
    		    		 });
    		    	 }
    		    }
    		          
    	    ]
    	});
    }
    
    //导出方法
    function exp() {
        //得到查询条件中的value和创建表单
        
        var name = trimSpace($('#name').val()),
		    category = $('#category').combobox('getValue');
    	    //startTime = trimSpace($('#startTime').val()),
    	    //stopTime = trimSpace($('#stopTime').val());
        
        var form = document.createElement('form');//创建表单
        form.method = 'post';//表单提交方式
        form.action = CONST_URL.EXP;//路径
        document.body.appendChild(form);

        //创建查询条件(input隐藏)
        //全称
        if ( name ) {
            var inputName = document.createElement('input');
            inputName.type = 'hidden';
            inputName.value = name;
            inputName.name = 'name';
            form.appendChild(inputName );
        }
        
        if ( category ) {
            var inputCategory = document.createElement('input');
            inputCategory.type = 'hidden';
            inputCategory.value = category;
            inputCategory.name = 'category';
            form.appendChild(inputCategory);
        }
        		       
        //如果没有任何条件就提示用户
        if ( name || category  ) {
            form.submit();
        } else {
            $.sdialog({
                 type : 'confirm',
                 msg : '未添加导出条件，默认为全部导出，确定吗？',
                 onConfirm : function(result) {
                     if (result) {
                         form.submit();
                     }
                 }
             });
        }
    }
    
   
   
</script>
</body>
</html>